﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{

    public class DbSet<T> : SimpleClient<T> where T : class, new()
    {
        public DbSet(SqlSugarClient context) : base(context)
        {

        }
        //SimpleClient中的方法满足不了你，你可以扩展自已的方法
        public List<T> GetList(List<IConditionalModel> conditionalList)
        {
            return Context.Queryable<T>().Where(conditionalList).ToList(); 
        }


        //SimpleClient中的方法满足不了你，你可以扩展自已的方法
        public List<T> GetByIds(dynamic[] ids)
        {
            return Context.Queryable<T>().In(ids).ToList(); ;
        }

        /// <summary>
        /// 根据条件更新
        /// </summary>
        /// <param name="updateObj"></param>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public bool Update(T updateObj, Expression<Func<T, bool>> whereExpression)
        {
            return this.Context.Updateable(updateObj).Where(whereExpression).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 复杂的查询用SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataTable GetTableBySQL(string sql, object parameters)
        {
            return Context.Ado.GetDataTable(sql, parameters);
        }

        /// <summary>
        ///  SQL查询单个
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object GetScalarBySQL(string sql, object parameters)
        {
            return Context.Ado.GetScalar(sql, parameters);
        }

        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public int ExecuteCommand(string sql, object parameters)
        {
            return Context.Ado.ExecuteCommand(sql, parameters);
        }

        /// <summary>
        /// 根据条件更新, 不更新 fileName 字段
        /// </summary>
        /// <param name="updateObj"></param>
        /// <param name="whereExpression"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool UpdateNoFiled(T updateObj, Expression<Func<T, bool>> whereExpression,string fileName)
        {
            return this.Context.Updateable(updateObj).IgnoreColumns(u => u == fileName).Where(whereExpression).ExecuteCommand() > 0;
        }



        /// <summary>
        /// 根据条件更新, 不更新 ignoreName字段
        /// </summary>
        /// <param name="updateObj"></param> 
        /// <returns></returns>
        public bool UpdateNoFiled(T updateObj, string ignoreName)
        {
            return this.Context.Updateable(updateObj).IgnoreColumns(u => u == ignoreName).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据条件更新, 不更新传入的集合列字段， 区分大小写
        /// </summary>
        /// <param name="updateObj"></param> 
        /// <returns></returns>
        public bool UpdateNoList(T updateObj, List<string> list)
        {
            return this.Context.Updateable(updateObj).IgnoreColumns(it => list.Contains(it)).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据条件更新,  不更新传入的集合列字段， 区分大小写
        /// </summary>
        /// <param name="updateObj"></param>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public bool UpdateNoList(T updateObj, Expression<Func<T, bool>> whereExpression, List<string> list)
        {
            return this.Context.Updateable(updateObj).IgnoreColumns(it => list.Contains(it)).Where(whereExpression).ExecuteCommand() > 0;
        }

        //传入为NUll的 不更新
        public bool UpdateNoNull(T updateObj, List<string> list)
        {
            return this.Context.Updateable(updateObj).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand() > 0;
        }

    }

    //创建一个DbContext类，使用DbSet(或者SimpleClient)
    public class DbContext
    {
        public DbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true,       //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
                InitKeyType = InitKeyType.SystemTable    //默认SystemTable, 字段信息读取, 如：该属性是不是主键，是不是标识列等等信息
            });


        }
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作 
    }
}
